const { getPool } = require('../../../config/db')

const HanziStructure = {
  async getAllHanziStructures() {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM hanzi_structure')
    return rows
  },

  /**
   * 根据汉字结构 ID 获取汉字结构信息
   *
   * @async
   * @param {number} id - 汉字结构ID
   * @returns {Object} 汉字结构信息对象
   */
  async getHanziStructureById(id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM hanzi_structure WHERE id = ?', [id])
    return rows[0]
  },

  async createHanziStructure(structureData) {
    const mysqlPool = await getPool()
    const [result] = await mysqlPool.execute(
      'INSERT INTO hanzi_structure (name, description) VALUES (?, ?)',
      [structureData.name, structureData.description]
    )
    return result.insertId
  },

  async updateHanziStructure(id, structureData) {
    const mysqlPool = await getPool()
    await mysqlPool.execute(
      'UPDATE hanzi_structure SET name=?, description=? WHERE id=?',
      [structureData.name, structureData.description, id]
    )
  },

  async deleteHanziStructure(id) {
    const mysqlPool = await getPool()
    await mysqlPool.execute('DELETE FROM hanzi_structure WHERE id = ?', [id])
  },
}

module.exports = HanziStructure